package com.hnust.controller;

import com.hnust.domain.Student;
import com.hnust.domain.Teacher;
import com.hnust.domain.record.CourseGroupInfo;
import com.hnust.domain.record.EditCourseGroupInfoRecord;
import com.hnust.service.ImportService;
import com.hnust.service.StudentService;
import com.hnust.service.TeacherService;
import com.hnust.service.TokenService;
import com.hnust.util.Result;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.util.*;

/**
 *创建人：游磊
 *创建时间：2020/10/7
 *描述：课程管理批量导入
 */
@CrossOrigin("*")
@RestController
@RequestMapping("/api/import")
public class Import {

    @Autowired
    TokenService tokenService;

    @Autowired
    ImportService importService;

    @Autowired
    StudentService studentService;

    @Autowired
    TeacherService teacherService;


    /**
     * 下载课程组模板
     * @return
     */
    @GetMapping("/courseGroup/download")
    public Result<String> DownloadCourseGroupTemp(){
        //获取所有的该年纪所有的专业
        try {
            return new Result<>(1,"返回专业成功","");
        }catch (Exception e){
            return new Result<>(0,"返回专业失败:"+e.getMessage(),"");
        }
    }

    /**
     *  上传课程组文件
     * @param token token信息
     * @param file 上传的文件
     * @return
     */
    @GetMapping("/courseGroup/upload")
    public Result<List<CourseGroupInfo>> UploadCourseGroupFile(@RequestParam("token") String token, @RequestParam("file") File file){
        //获取所有的该年纪所有的专业
        try {
            return new Result<>(1,"返回专业成功",null);
        }catch (Exception e){
            return new Result<>(0,"返回专业失败:"+e.getMessage(),null);
        }
    }


    /**
     *  批量导入课程组信息
     * @param token token信息
     * @param courseGroupInfos 课程组信息
     * @return
     */
    @PostMapping("/courseGroup")
    public Result<String> BatchImportCourseGroupInfo(@RequestParam("token") String token, @RequestParam("file") List<CourseGroupInfo> courseGroupInfos){
        // 验证token信息
        if(!tokenService.verifyToken(token, "院管理员")){
            return new Result<>(0,"身份验证错误",null);
        }
        // 开始导入课程信息
        try {
            importService.BatchImportCourseGroupInfo(courseGroupInfos);
            return new Result<>(1,"返回专业成功",null);
        }catch (Exception e){
            return new Result<>(0,"返回专业失败:"+e.getMessage(),null);
        }
    }


    /**
     * 获取课程组信息
     * @param token token认证信息
     * @param colId 专业id
     * @return
     */
    @GetMapping("/courseGroupInfo")
    public Result<EditCourseGroupInfoRecord> GetCourseGroupInfo(@RequestParam("token") String token, @RequestParam("colId") String colId){
        // 验证token信息
        if(!tokenService.verifyToken(token, "院管理员")){
            return new Result<>(0,"身份验证错误",null);
        }
        try {
            return new Result<>(1,"返回专业成功",importService.GetCourseGroupInfo(colId));
        }catch (Exception e){
            return new Result<>(0,"返回专业失败:"+e.getMessage(),null);
        }
    }

    @PostMapping("student/import")
    public Result<Object> saveStudentToDB(@RequestBody Map<String,Object> map){
        JSONArray studentsJson = JSONArray.fromObject(map.get("students"));
        List<Student> students = new ArrayList<>(studentsJson.size()+1);
        for(int i=0; i<studentsJson.size();i++){
            JSONObject jsonObject = studentsJson.getJSONObject(i);
            students.add(new Student(
                    jsonObject.getString("stuId"),
                    jsonObject.getString("account"),
                    jsonObject.getString("alias"),
                    jsonObject.getString("name"),
                    jsonObject.getString("stuNumber"),
                    jsonObject.getString("phoNumber"),
                    null,
                    jsonObject.getString("password"),
                    jsonObject.getString("school"),
                    null
            ));
        }
        String token = (String)map.get("token");
        if(!tokenService.verifyToken(token, "院管理员")){
            return new Result<>(0,"身份验证错误",null);
        }
        try {
            return new Result<>(1,"导入学生成功",studentService.addStudents(students));
        }catch (Exception e){
            //e.printStackTrace();
            return new Result<>(0,"导入学生失败："+e.getMessage(),null);
        }
    }

    @PostMapping("teacher/import")
    public Result<Object> saveTeacherToDB(@RequestBody Map<String,Object> map){
        JSONArray teachersJson = JSONArray.fromObject(map.get("teachers"));
        List<Teacher> teachers = new ArrayList<>(teachersJson.size()+1);
        String collegeId = map.get("collegeId").toString();
        for(int i=0; i<teachersJson.size();i++){
            JSONObject jsonObject = teachersJson.getJSONObject(i);
            teachers.add(new Teacher(
                    jsonObject.getString("teaId"),
                    jsonObject.getString("teaNumber"),
                    jsonObject.getString("name"),
                    jsonObject.getString("password"),
                    jsonObject.getString("phoNumber"),
                    jsonObject.getString("email"),
                    jsonObject.getString("colId")
            ));
        }
        String token = (String)map.get("token");
        if(!tokenService.verifyToken(token, "院管理员")){
            return new Result<>(0,"身份验证错误",null);
        }
        try {
            return new Result<>(1,"导入学生成功",teacherService.addTeachers(teachers,collegeId));
        }catch (Exception e){
            //e.printStackTrace();
            return new Result<>(0,"导入学生失败："+e.getMessage(),null);
        }
    }

}
